<?php

namespace App\Http\Controllers\Wap;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Model\Member;
use App\Http\Controllers\Controller;

class MemberController extends Controller
{
    public function __construct(Request $request){
        $this->request = $request;
        $this->member = session('wechat_user');
//        $this->openid = 'oiYTrwfH-VLzRn8OkLsn_knKv3qM';
        if (!empty($this->member)) {
            $this->memberId = Member::where('openid',$this->member['id'])->pluck('id');
        }
    }

    /**
     * @return $this
     * 个人中心
     * czs
     */
    public function index(){
        $forPay = OrderList::where('member_id',$this->memberId)->where('status',1)->count();
        $forDelive = OrderList::where('member_id',$this->memberId)->where('status',2)->count();
        $forReceive = OrderList::where('member_id',$this->memberId)->where('status',3)->count();
        $res = Member::memberCenter($this->openid)->getData();
        $count = NewsMember::findWapNews($this->memberId)->getData();
        return view('wap.member.index')
            ->with('res',$res)
            ->with('forPay',$forPay)
            ->with('forDelive',$forDelive)
            ->with('forReceive',$forReceive)
            ->with('count',$count);
    }

    /**
     * 我的消息
     * czs
     */
    public function news(){
        $newsType = $this->request->input('id');
        $res = NewsMember::newsList($this->memberId,$newsType,$limit=6)->getData();
        $menu = NewsType::typePull()->getData();
        return view('wap.member.news')
            ->with('res',$res)
            ->with('menu',$menu);
    }

    /**
     * 中间跳转
     * czs
     */
    public function mid() {
        return redirect('/wap/goods/list?id=0');
    }
    /**
     * 消息详情
     * czs
     */
    public function newsDetail($id){
        News::hasView($this->memberId,$id);
        $res = News::getDetail($id)->getData();
        return view('wap.member.newsDetail')
            ->with('res',$res);
    }

    /**
     * 个人信息
     * czs
     */
    public function info(){
        if($this->request->isMethod('post')) {
            $input = $this->request->all();
            Member::saveInfo($input,$this->memberId);
        }
        $one = Member::getOne($this->memberId)->getData();
        return view('wap.member.info')
            ->with('one',$one);
    }
    public function viewWl(){
        return view('wap.member.viewWl');
    }

    /**
     * 绑定手机号
     * czs
     */
     public function bindTel(){
         $member = session('wechat_user');
         if($this->request->isMethod('post')) {
             $post = $this->request->all();
             $res = Member::wapBindTel($post,$member['id'],'abc');
             return $res->getResult();
         } else {
             return view('wap.member.bindTel');
         }
    }
    //更换手机号
	public function changeTel(){
         if($this->request->isMethod('post')) {
             $post = $this->request->all();
             $res = Member::webchangeTel($post,$this->memberId);
             return $res->getResult();
         } else {
             return view('wap.member.changeTel');
         }
    }
    /**
     * @return $this
     * 购物车
     * czs
     */
    public function cart($id){
        $res = ShoppingCart::cartDetail($id)->getData();
        return view('wap.member.cart')
            ->with('res',$res)
            ->with('memberId',$this->memberId);
    }

    /**
     * 删除货物
     * czs
     */
    public function deleteGoods() {
        $id = $this->request->input('id');
        $res = ShoppingCart::deleteOne($id);
        return $res->getResult();
    }

    /**
     * 添加货物
     * czs
     */
    public function addGoods() {
        if (empty($this->member)) {
            return [
                'success'=>false,
                'type'=>9
            ];
        }
        $input = $this->request->all();
        $memberId = $this->memberId;
        $res = ShoppingCart::addGoods($input,$memberId);
        return $res->getResult();
    }
    
}
